package cn.jiyun.user.service.impl;

import cn.jiyun.user.mapper.UserMapper;
import cn.jiyun.user.pojo.User;
import cn.jiyun.user.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String findAll() throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();

        String userListStr = (String) redisTemplate.boundValueOps("user_list").get();
if (StringUtils.isEmpty(userListStr)){
    List<User> userList = userMapper.selectList(null);
    userListStr = objectMapper.writeValueAsString(userList);
    redisTemplate.boundValueOps("user_list").set(userListStr);
    System.out.println("查询数据库");

}else {
    System.out.println("查询redis");
}



        return userListStr;
    }



}
